package fr.mywiki.business.file;

import java.rmi.RemoteException;

import fr.mywiki.model.ejbAdapter.Value;
import fr.mywiki.model.queries.DatabaseFileController;

/**
 * This class represents a file in the database. It can have the form of a file
 * descriptor of can also contain a complete file.
 * 
 * @author tlombard
 */
public class FileLite extends Value {
	private static final long serialVersionUID = 1L;
	
	public static final String JNDI_NAME = "MyWiki/File";

	/** The default object type */
	public static final String TYPE_ENT = "file";
	
	/**
	 * The object type which enables the use of limited file objects (only file
	 * descriptors) without the content of the file.
	 */
	public static final String TYPE_ENT_LIGHT = "fileLight";
	
	/* The fields */
	public static final String FIELD_NAME = "name";
	public static final String FIELD_CONTENT = "content";
	
	/** Identifier of the document */
	private Long id;
	
	/**
	 * The name of the document, the complete name must be unique in the whole
	 * document table.
	 */
	private String name;
	
	/** The content of the file */
	private byte[] content;
	
	/**
	 * Specifies if the value object represented here is a light (only a
	 * descriptor) or a heavy (with the data) file object.
	 */ 
	private boolean lightValue;

	/** Empty constructor. */
	public FileLite() {}
	
	/**
	 * Complete constructor of the <code>FileLite</code> object.
	 * @param id identifier of the file
	 * @param name name of the file
	 * @param content content of the file
	 */
	public FileLite(Long id, String name, byte[] content) {
		this.id = id;
		this.name = name;
		this.content = content;
		lightValue = false;
	}
	
	/**
	 * Light constructor of the <code>FileLite</code> object. Returns a file
	 * descriptor.
	 * 
	 * @param id identifier of the file
	 * @param name name of the file
	 * TODO test it !
	 */
	public FileLite(Long id, String name) {
		this.id = id;
		this.name = name;
		lightValue = true;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTypeEnt() {
		return TYPE_ENT;
	}

	public Object get(String attName) {
		if (FIELD_NAME.equals(attName))
			getName();
		return null;
	}

	public void set(String attName, Object value) {
		if(FIELD_NAME.equals(attName))
			setName((String)value);
	}

	public byte[] getContent() {
		if (isLightValue())
			populate();
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public boolean isLightValue() {
		return lightValue;
	}
	
	/**
	 * Makes the object <i>heavy</i> if it was not.
	 */
	private void populate() {
		if(isLightValue())
			try {
				setContent(DatabaseFileController.getFileContent(this));
				lightValue = false;
			} catch (RemoteException e) {
				e.printStackTrace();
			}
	}
}
